package com.epoint.mobileoa.ui.ds;



import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.ConfigKey;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.bizlogic.Task;
import com.epoint.mobileoa.bizlogic.mobileoa.SynchroOrganizationPagingFileTask;
import com.epoint.mobileoa.bizlogic.mobileoa.SynchroOrganizationPagingFileTaskFull;
import com.epoint.mobileoa.bizlogic.mobileoa.SynchroOrganizationPagingTask;
import com.epoint.mobileoa.bizlogic.mobileoa.SynchroOrganizationTask;
import com.epoint.mobileoa.db.DBHelp;
import com.epoint.mobileoa.service.MainService;
import com.epoint.mobileoa.tools.PropertiesHelp;
import com.epoint.mobileoa.tools.StringHelp;
import com.epoint.mobileoa.tools.ToastHelp;

import android.view.View;
import android.webkit.WebView;
import android.widget.Button;


/**
 * @author liyc
 * @time 2012-1-8 下午9:57:02
 * @annotation 
 */
public class DSView extends SuperView {

	private Button ds_bt;
	private WebView ds_wv_info;
	public static final int SynchroOrganization = 1;//同步组织架构
	public static int isDS = 0;//0没有执行同步，1正在执行同步

	@Override
	public void initControls() {
		ds_bt = (Button) findViewById(R.id.ds_bt);
		ds_wv_info = (WebView) findViewById(R.id.ds_wv_info);
		ds_bt.setOnClickListener(this);
		ds_wv_info.setBackgroundColor(0);
		String dsinfo = DBHelp.getConfigValue(this, ConfigKey.dsinfo);
		ds_wv_info.loadDataWithBaseURL(null, dsinfo,"text/html","utf-8",null);
	}

	@Override
	public void refresh(Object... param) {
		if(param[0].toString().equals(MainService.NORMAL)){
			String msg = param[1]==null?"":param[1].toString();
			System.out.println(msg);
			String result = StringHelp.getXMLAtt(msg, "result");
			if(result.indexOf("true")>-1){
				ds_wv_info.loadDataWithBaseURL(null, StringHelp.getXMLAtt(msg, "info"),"text/html","utf-8",null);
//				ToastHelp.showToastCommon(this, "组织架构同步成功！");
			}
			else{
				ToastHelp.showToast(this, "同步失败！", 1000);
			}
		}
		isDS = 0;
		top_pb.setVisibility(View.GONE);
		if(m_Dialog!=null){
			m_Dialog.dismiss();
		}
	}

	@Override
	public int getLayoutInt() {
		return R.layout.ds;
	}
	

	@Override
	public String setTitle() {
		return getString(R.string.sjtb);
	}

	@Override
	public void onClick(View v) {

		if(v.getId()==ds_bt.getId()){
			if(isDS==1){
				ToastHelp.showToast(this, "正在执行同步中！", 1000);
			}
			else{
				isDS = 1;
				top_pb.setVisibility(View.VISIBLE);
				ds(this);
			}
		}
	}

	public static void ds(SuperView con){
		ToastHelp.showToastCommon(con, "正在同步组织架构...");
		String dstype = PropertiesHelp.getProperty(con, "dstype");
		Task task = null;
		if(dstype.equals("0")){
			task = new SynchroOrganizationTask(con,null);
			MainService.addTask(task);
		}
		else if(dstype.equals("1")){
			task = new SynchroOrganizationPagingTask(con,null);
		}
		else if(dstype.equals("2")){
			task = new SynchroOrganizationPagingFileTask(con,null);
		}
		else if(dstype.equals("3")){
			task = new SynchroOrganizationPagingFileTaskFull(con,null);
		}
		MainService.addTask(task);
	}

	@Override
	public void refreshForNewTask(int TaskId,Object... param) {
	}
}
